/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openjawtech.xscraper.scraper.runtime.xml;

import com.openjawtech.xscraper.scraper.exception.ScrapeException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

/**
 *
 * @Created on 17-Apr-2009, 22:16:53
 * @author Wenbo.Sun
 */
public class ScrapeXpath {

    private XPath xPath;

    public ScrapeXpath() {
        xPath = XPathFactory.newInstance().newXPath();
    }

    public String evaluateStr(String path, Document doc) throws ScrapeException {
        String result = null;
        try {
            result = (String) xPath.evaluate(path, doc, XPathConstants.STRING);
        } catch (Throwable t) {
            throw new ScrapeException("evaluate xpath failed!", t);
        }
        return result;
    }

    public NodeList evaluateNodes(String path, Document doc) throws ScrapeException {
        NodeList result = null;
        try {
            result = (NodeList) xPath.evaluate(path, doc, XPathConstants.NODESET);
        } catch (Throwable t) {
            throw new ScrapeException("evaluate xpath failed!", t);
        }
        return result;
    }
}
